package com.coder.oj.admin.controller;

import com.coder.oj.admin.mapstruct.UserMapstruct;
import com.coder.oj.admin.query.UserQuery;
import com.coder.oj.admin.service.UserService;
import com.coder.oj.admin.vo.UserVo;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.core.JwtToken;
import com.coder.oj.common.core.R;
import com.coder.oj.common.entity.User;
import com.coder.oj.common.enums.CommonStatusEnum;
import com.coder.oj.common.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserMapstruct userMapstruct;

    @JwtToken
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public R<JsonPage<UserVo>> getUserPage(@RequestBody UserQuery params) {
        return R.ok().setResult(userService.getUserPage(params));
    }

    @JwtToken
    @GetMapping
    @ApiOperation("用户详情")
    public R<UserVo> getUserById(@RequestParam("id") Long id) {
        return R.ok().setResult(userMapstruct.toVO(userMapper.selectById(id)));
    }

    @JwtToken
    @PutMapping
    @ApiOperation("禁用启用")
    public R updateUserStatus(@RequestParam("id") Long id) {
        User user = userMapper.selectById(id);
        if(CommonStatusEnum.getEnum(user.getStatus()) == CommonStatusEnum.ENABLED) {
            user.setStatus(CommonStatusEnum.DISABLED.getValue());
        } else {
            user.setStatus(CommonStatusEnum.ENABLED.getValue());
        }
        userMapper.updateById(user);
        return R.ok();
    }
}
